//
//  HMProductViewController.m
//  01-网易彩票
//
//  Created by SZSYKT_iOSBasic_2 on 16/2/22.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "HMProductViewController.h"
#import "HMProduct.h"
#import "HMProductCell.h"
@interface HMProductViewController()
/**
 *  所有的产品模型
 */
@property (nonatomic, strong) NSMutableArray *products;
@end

@implementation HMProductViewController
//reason: 'UICollectionView must be initialized with a non-nil layout parameter'
// 原因：collectionView必须使用一个非空的布局参数初始化

// reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:'
// 原因：必须注册一个cell
- (instancetype)init {
    // 创建流水布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item尺寸
    CGFloat itemW = 80;
    CGFloat itemH = itemW;
    flowLayout.itemSize = CGSizeMake(itemW, itemH);
    // 设置每一列之间的间隙
    flowLayout.minimumInteritemSpacing = 0;
    // 设置每一行之间的间隙
    flowLayout.minimumLineSpacing = 20;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 0, 0, 0);
    return [super initWithCollectionViewLayout:flowLayout];
}

static NSString *ID = @"product";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置背景颜色
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // 注册cell
//    [self.collectionView registerClass:[HMProduct class] forCellWithReuseIdentifier:ID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HMProductCell" bundle:nil] forCellWithReuseIdentifier:ID];
}

#pragma mark - UICollectionView 数据源方法
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    return 2;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 创建cell
//    UICollectionViewCell *cell = [[UICollectionViewCell alloc] init];
    HMProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
    // 传递模型
    cell.product = self.products[indexPath.item];
    // 返回cell
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 获得模型
    HMProduct *product = self.products[indexPath.item];
    // 跳转到A应用
    UIApplication *app = [UIApplication sharedApplication];
    
    // 获得应用的本地URL
    NSURL *localURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@",product.customUrl,product.Id]];
    if ([app canOpenURL:localURL]) { // 判断本地是否安装了应用
        [app openURL:localURL];
    } else {
        // url: http://
        [app openURL:[NSURL URLWithString:product.url]];
    }
}

#pragma mark - 懒加载数据
- (NSMutableArray *)products {
    if (_products == nil) {
        // 创建模型数组
        _products = [NSMutableArray array];
        
        // 将文件数据转换成二进制数据
        NSArray *productArray = HMJsonData(@"more_project.json");
        //[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_project.json" ofType:nil]] options:NSJSONReadingMutableContainers error:NULL];
        // 遍历数组
        for(NSDictionary *dict in productArray) {
            // 字典转模型
            HMProduct *product = [HMProduct productWithDict:dict];
            // 将模型添加到数组中
            [_products addObject:product];
        }
    }
    return _products;
}
@end
